package sort;

import java.util.List;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Collections;

public class ComparatorTest {

    public static void main(String[] args) {

        List<Employee> staff = new ArrayList<>();

        staff.add(new Employee(20, "Tom"));
        staff.add(new Employee(23, "Bob"));
        staff.add(new Employee(22, "Carl"));
        staff.add(new Employee(25, "Andy"));
        staff.add(new Employee(21, "Alice"));

        Comparator<Employee> comparator = new Comparator<Employee>() {
            public int compare(Employee a, Employee b) {
                return a.getAge() - b.getAge();
            }
        };

        Collections.sort(staff, comparator);

        // Print Result.
        for (Employee employee : staff) {
            System.out.println(employee);
        }

        Collections.sort(staff, Collections.reverseOrder(comparator));

        // Print Result.
        for (Employee employee : staff) {
            System.out.println(employee);
        }
    }
}

class Employee {

    private int age;
    private String name;

    public Employee(int age, String name) {
        this.age = age;
        this.name = name;
    }

    public int getAge() {
        return this.age;
    }

    public String toString() {
        return "[name: " + this.name + "\t, age: " + this.age + "]";
    }
}
